<template>
  <div>
    <!-- 三级分类全局组件 -->
    <!-- 父子组件通信，父scene传给儿子 -->
    <Category :scene="scene" />
    <el-card style="margin: 10px 0px;">
      <div v-show="scene == 0">
        <el-button type="primary" size="default" icon="Plus" :disabled="categoryStore.c3Id ? false : true"
          @click="addAttr">添加属性</el-button>
        <el-table border style="margin: 10px 0px" :data="attrArr">
          <el-table-column label="序号" type="index" width="60px" align="center"></el-table-column>
          <el-table-column label="属性名称" width="100px" align="center" prop="attrName"></el-table-column>
          <el-table-column label="属性值名称">
            <template #="{ row, $index }">
              <el-tag style="margin:5px" v-for="(item, index) in row.attrValueList" :key="item.id">{{
                item.valueName }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="120px" align="center">
            <template #="{ row, $index }">
              <!-- 修改已有属性的按钮 -->
              <el-button type="primary" size="small" icon="Edit" @click="updateAttr"></el-button>
              <el-button type="danger" size="small" icon="Delete"></el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div v-show="scene == 1">
        <el-form :inline="true">
          <el-form-item label="属性名称">
            <el-input placeholder="请输入属性名称" v-model="attrParams.attrName"></el-input>
          </el-form-item>
        </el-form>
        <el-button :disabled="attrParams.attrName ? false : true" type="primary" size="default" icon="Plus"
          @click="addAttrValue">添加属性值</el-button>
        <el-button type="danger" size="default" @click="cancel">取消</el-button>
        <el-table border style="margin: 10px 0px;" :data="attrParams.attrValueList">
          <el-table-column width="80px" label="序号" type="index" align="center"></el-table-column>
          <el-table-column label="属性值名称">
            <!-- row:为当前属性值对象 -->
            <template #="{ row, $index }">
              <el-input v-if="row.flag" @blur="showDiv(row, $index)" placeholder="请输入属性值名称"
                v-model="row.valueName"></el-input>
              <div v-else @click="showInput(row)">{{ row.valueName }}</div>
            </template>
          </el-table-column>
          <el-table-column width="120px" label="操作" align="center"></el-table-column>
        </el-table>
        <el-button type="primary" size="default" @click="save"
          :disabled="attrParams.attrValueList.length > 0 ? false : true">保存</el-button>
        <el-button type="primary" size="default" @click="cancel">取消</el-button>
      </div>
    </el-card>
  </div>
</template>
  
<script setup lang="ts">
import { watch, ref, reactive } from 'vue'
import { reqAttr, reqAddOrUpdateAttr } from '@/api/product/attr'
import { AttrResponseData, Attr, AttrValue } from '@/api/product/attr/type'
import useCategoryStore from '@/store/modules/category'
import { ElMessage } from 'element-plus'
let categoryStore = useCategoryStore()
//存储返回的属性与属性值数据
let attrArr = ref<Attr[]>([]);
//定义显示card组件内容，0显示table,1显示添加与修改界面
let scene = ref<number>(0);
//收集新增属性数据
let attrParams = reactive<Attr>({
  attrName: "",
  attrValueList: [],
  categoryId: '', //三级分类的id
  categoryLevel: 3
})

//添加属性按钮回调
const addAttr = () => {
  //添加之前，需要将上一次添加的数据清空
  Object.assign(attrParams, {
    attrName: "",
    attrValueList: [],
    categoryId: categoryStore.c3Id, //三级分类的id
    categoryLevel: 3
  })
  //切换添加与修改界面
  scene.value = 1

  //收集三级分类id
  // attrParams.categoryId = categoryStore.c3Id
}
//table中修改按钮的回调
const updateAttr = () => {
  scene.value = 1
}
//取消按钮的回调
const cancel = () => {
  scene.value = 0
}

//监听三级分类id,发送请求
watch(() => categoryStore.c3Id, () => {
  //需要清空上一次查询的数据
  attrArr.value = []
  //需要确保有三级分类的id，才能发请求
  if (!categoryStore.c3Id) return;
  getAttr()

})

//获取属性与属性值
const getAttr = async () => {
  const { c1Id, c2Id, c3Id } = categoryStore
  let result: AttrResponseData = await reqAttr(c1Id, c2Id, c3Id)
  if (result.code == 200) {
    attrArr.value = result.data
  }
}

//添加属性值按钮回调
const addAttrValue = () => {
  //点击按钮时，向数据添加一个属性值对象
  attrParams.attrValueList.push({
    valueName: '',
    flag: true  //用于切换编辑与显示:true展示input，false展示div
  })
}

const showDiv = (row: AttrValue, $index: number) => {
  //非法情况1:添加的属性值不能为空
  if (row.valueName.trim() == "") {
    //从attrValueList列表中删除属性值为空的元素
    attrParams.attrValueList.splice($index, 1)
    ElMessage({
      type: "error",
      message: "属性值不能为空！"
    })
    return
  }
  //非法情况2:添加的属性值不能重复
  let repeat = attrParams.attrValueList.find((item) => {
    //需要将当前失去焦点的属性值对象从数组中去掉
    if (item != row) {
      return item.valueName === row.valueName
    }
  })
  if (repeat) {
    //从attrValueList列表中删除属性值重复的元素
    attrParams.attrValueList.splice($index, 1)
    ElMessage({
      type: "error",
      message: "属性值不能重复！"
    })
    return
  }

  row.flag = false
}
const showInput = (row: AttrValue) => {
  row.flag = true
}

//保存按钮的回调
const save = async () => {
  let result: any = await reqAddOrUpdateAttr(attrParams)
  if (result.code == 200) {
    //切换界面
    scene.value = 0
    ElMessage({
      type: 'success',
      message: attrParams.id ? "修改成功" : "添加成功"
    })
    //更新一下
    getAttr()
  } else {
    ElMessage({
      type: 'error',
      message: attrParams.id ? "修改失败" : "添加失败"
    })
  }
}

</script>
  
<style scoped></style>
